package JFrame;

import entity.Sc;
import serive.ScService;
import serive.ScServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.List;

public class SnackRanking extends JFrame {
    JTable t_sc;
    ScService scService = new ScServiceImpl();

    public SnackRanking() {
        this.setBounds(200,300,500,500);
        this.setTitle("贪吃蛇——前10排行榜");
        init();
        this.setVisible(true);
    }

    public void init() {
        JLabel jLabel = new JLabel("前10榜");
        jLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        jLabel.setBounds(200, 18, 69, 45);
        this.getContentPane().add(jLabel);

        Object[] title = {"时间", "用户id", "成绩", "游戏"};
        Object[][] scs = new String[10][4];
        List<Sc> scsses = scService.queryAllSc();
        Iterator<Sc> iter = scsses.iterator();
        int j = 0;
        Sc next = iter.next();
        for (int i = 0; i < 10; i++) {
            scs[i][j] = next.getsId();
            j++;
            scs[i][j] = next.getuId();
            j++;
            scs[i][j] = next.getScore();
            j++;
            scs[i][j] = next.getGameType();
            j = 0;
            if (iter.hasNext()) {
                next = iter.next();
            } else {
                break;
            }
        }

        JScrollPane pane = new JScrollPane();
        t_sc = new JTable(scs, title);
        this.setLayout(null);
        pane.setBounds(50, 50, 400, 190);
        pane.getViewport().add(t_sc);
        this.getContentPane().add(pane);
    }
}
